﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using WorkRecorder.Business;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization;

namespace WorkRecorder.Data
{
     
    public class DataMapperXmlImpl : IDataMapper<Records<Record>> 
    { 
        //[InternalsVisibleToAttribute("WorkRecorderTestProject")]
 

        public void SaveGraph(Records<Record> graph, string path)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(Records<Record>));
            using (StreamWriter sw = new StreamWriter(path))
            {
                serializer.Serialize(sw, graph);
            }
        }
 

        public Records<Record> ReadGraph(string path)
        {
            XmlSerializer deserializer = new XmlSerializer(typeof(Records<Record>));

            if (File.Exists(path))
            {
                using (StreamReader sr = new StreamReader(path))
                {
                    var result = deserializer.Deserialize(sr) as Records<Record>;
                    return result;
                }
            }
            else
            {
                throw new FileNotFoundException("Could not find data file at path: "+path);
            }
        }

 
    }
}
